package com.wanmait.hotelManage.webservlet;


import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64.Decoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.HotelCommentDAO;
import com.wanmait.hotelManage.dao.PriceDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.vo.HotelComment;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Price;
import com.wanmait.hotelManage.vo.RoomType;

/**
 * Servlet implementation class loginServerlet
 */
@WebServlet("/indexServlet")
public class indexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public indexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("hot".equals(action)) {
			this.roomHot(request, response);
		}
		else if("hotelComment".equals(action)) {
			this.hotelComment(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else {
			this.roomHot(request, response);
		}
	}
	//搜索
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

		String roomType = request.getParameter("roomType");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("=====");
		System.out.println(startTime);
		System.out.println(endTime);
		System.out.println(dateFormat);
		Date st=null;
		Date et=null;
		try {
			st=dateFormat.parse(startTime);
			et=dateFormat.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		Timestamp tst=new Timestamp(st.getTime());
		Timestamp tet=new Timestamp(et.getTime());
		//获得入住的每一天
		List<Timestamp> timestamps=new ArrayList<Timestamp>();
		while(true) {
			if(tst.before(tet)) {
				timestamps.add(tst);
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(tst);
				startCalendar.add(startCalendar.DATE, 1);
				tst = new Timestamp(startCalendar.getTimeInMillis());
			}else {
				break;
			}
		}
		//判断房间类型
		if("房间类型".equals(roomType)) {//搜索全部的房间类型
			//获得全部房间类型
			List<RoomType> allroomTypes=(List<RoomType>)RoomTypeDAO.getRoomtypedao().findAll();
			Pager pager = new Pager();
			pager.setPageSize(2);//每页显示的记录数
			int rows = RoomTypeDAO.getRoomtypedao().getSearchRows(allroomTypes,timestamps);//总行数
			pager.setPageCount(rows);//总页数
			pager.setPageNo(1);//默认显示第一页
			//获得符合条件的房间类型
			List<RoomType> roomTypes=RoomTypeDAO.getRoomtypedao().search(allroomTypes,timestamps,pager);
		
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", endTime);
			request.setAttribute("pager",pager);
			request.setAttribute("roomTypes", roomTypes);
			request.getRequestDispatcher("/FrontDesk/searchRoomList.jsp").forward(request, response);
		}else {//指定房间类型搜索
			Boolean flag=RoomTypeDAO.getRoomtypedao().search(roomType,timestamps);
			if(flag) {//有空房
				response.setCharacterEncoding("utf-8");
				roomType = URLEncoder.encode(roomType);
				request.setAttribute("startTime", startTime);
				request.setAttribute("endTime", endTime);
				request.getRequestDispatcher("/roomListServlet?action=roomDetail&roomTypeName="+roomType).forward(request, response);
	//			response.sendRedirect(request.getContextPath()+"/roomListServlet?action=roomDetail&roomTypeName="+roomType);
			}else {//没空房
				response.setCharacterEncoding("utf-8");
				roomType = URLEncoder.encode(roomType);
				request.setAttribute("startTime", startTime);
				request.setAttribute("endTime", endTime);
				request.setAttribute("msg", "no");
				request.getRequestDispatcher("/roomListServlet?action=roomDetail&roomTypeName="+roomType).forward(request, response);
		}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void hotelComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{ List<HotelComment> hotelComments=HotelCommentDAO.getHotelcommentdao().hotelComment();
	
	
	
	  
		
	}
	public void roomHot(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		List<HotelComment> hotelComments=HotelCommentDAO.getHotelcommentdao().hotelComment();
		List<RoomType> roomTypes=RoomTypeDAO.getRoomtypedao().hotRoom();
		List<RoomType> roomtypes = RoomTypeDAO.getRoomtypedao().findAll();
		List<Price> prices=PriceDAO.getdao().findAll();
		if(roomTypes==null||hotelComments==null) {
			
		}else {
			request.setAttribute("prices",prices );
			request.setAttribute("HotelComments",hotelComments );
			request.setAttribute("roomTypes",roomTypes);
			request.setAttribute("roomtypes", roomtypes);
			RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
			rd.forward(request, response);
			
			
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
